<?php

namespace app\shop\controller\cash;

use app\common\enum\user\UserCashStatusEnum;
use app\shop\controller\Controller;
use app\common\library\helper;
use app\common\model\settings\Setting;
use app\common\model\user\User;
use app\common\model\user\UserCash;
use app\shop\model\order\Order as OrderModel;
use app\shop\model\supplier\Supplier as SupplierModel;
/**
 * 提现
 */
class Cash extends Controller
{
    /**
     * 首页概况
     */
    public function index()
    {
        // 平台统计数据
        $tj_data = [
            'total_money' => helper::number2((new OrderModel())->getTotalMoney('all')),
            'supplier_money' => helper::number2((new OrderModel())->getTotalMoney('supplier')),
            'sys_money' => helper::number2((new OrderModel())->getTotalMoney('sys')),
        ];
        // 供应商统计数据
        $supplier_data = [
            'total_money' => helper::number2((new SupplierModel())->getTotalMoney('total_money')),
            'money' => helper::number2((new SupplierModel())->getTotalMoney('money')),
            'nosettled_money' => helper::number2((new OrderModel())->getTotalMoney('supplier', 0)),
            'freeze_money' => helper::number2((new SupplierModel())->getTotalMoney('freeze_money')),
            'cash_money' => helper::number2((new SupplierModel())->getTotalMoney('cash_money')),
            'deposit_money' => helper::number2((new SupplierModel())->getTotalMoney('deposit_money')),
        ];
        return $this->renderSuccess('', compact( 'tj_data', 'supplier_data'));
    }

    public function apply_status()
    {
        $lists = UserCashStatusEnum::data();
        return $this->renderSuccess('', $lists);
    }

    public function apply_lists(){
        $map = [];
        $param = $this->postData();
        $apply_status = $param['apply_status'] ?? -1;
        $is_done = $param['is_done'] ?? 0;
        if($is_done){
            $map[] = ['apply_status', 'in', [UserCashStatusEnum::REFUSE, UserCashStatusEnum::CANCEL,UserCashStatusEnum::RECEIVED]];
        }
        else{
            $map[] = ['apply_status', 'in', [UserCashStatusEnum::WAIT, UserCashStatusEnum::PASS,UserCashStatusEnum::REFUND]];
        }
        if ($apply_status >0) {
            $map[] = ['apply_status', '=', $apply_status];
        }
        $pay_type = $param['pay_type'] ?? -1;
        if($pay_type>0){
            $map[] = ['pay_type', '=', $pay_type];
        }
        $search = $param['search'] ?? '';
        if($search){
            if(is_numeric($search)){
                $map[] = ['user_id', '=', $search];
            }
            else{
                $map[] = ['alipay_name', 'like', '%'.$search.'%'];
            }
        }
        $page_size = $param['page_size'] ?? 10;
        $lists = UserCash::where($map)->with(['user'])->order('id', 'desc')->paginate($page_size);
        return $this->renderSuccess('获取成功', $lists);
    }

    /**
     * 审核
     */
    public function apply_audit(){
        $param = $this->postData();
        $apply_status = $param['apply_status'] ?? UserCashStatusEnum::PASS;
        $id = $param['id'] ?? 0;
        if(!$id){
            return $this->renderError('参数错误');
        }
        $model = UserCash::find($id);
        if(!$model){
            return $this->renderError('参数错误');
        }
        if($model['apply_status']['value']!=UserCashStatusEnum::WAIT){
            return $this->renderError('状态错误');
        }
        $user = User::find($model['user_id']);
        $model->startTrans();
        try{
            $model->apply_status = $apply_status;
            $model->audit_time = time();
            if($apply_status==UserCashStatusEnum::PASS){
                //计算手续费
                $setting = Setting::getItem('balance_cash');
                $deduct = $setting['deduct'];
                $cash_broke = round($model['money'] * $deduct/100,2);
                $model->cash_broke = $cash_broke;
                $model->real_money = $model['money']-$cash_broke;
            }
            else{
                $reject_reason = $param['reject_reason'] ?? '';
                if(!$reject_reason){
                    $model->rollback();
                    return $this->renderError('请填写拒绝原因');
                }
                $model->reject_reason = $param['reject_reason'] ?? '';
                //返回用户余额
                $user->freeze_money -= $model['money'];
                $user->balance += $model['money'];
                $user->save();
            }
            if($model->save()){
                $model->commit();
                return $this->renderSuccess('操作成功');
            }
            else{
                $model->rollback();
                return $this->renderError($model->getError() ?? '操作失败');
            }
        }
        catch  (\Exception $e){
            $model->rollback();
            return $this->renderError($e->getMessage());
        }
    }
}
